<?php
namespace Apps\back\Controllers;
use Apps\Back\Controllers\ControllerBase;
use Apps\Model\Admin;
use Apps\Model\AdminLoginLog as AdminLogin;
use Phalcon\Filter as Filter;


class IndexController extends ControllerBase{

    function indexAction(){
     
    }

    function loginAction(){
    	if($this->request->isPost()){
           $name=$this->request->getPost('name','string');
           $pwd=$this->request->getPost('pwd','string');
           $filter = new Filter();
           $name = $filter->sanitize($name, "trim");
           $pwd = $filter->sanitize($pwd, "trim");

           if(empty($name) || empty($pwd)){
                $this->jump("admin/index/index",'用户和密码不能为空!');
                return;
            }
            $row=Admin::findFirst("name='$name'");
            if(empty($row)){
            	$this->jump("admin/index/index",'用户名不存在!');
                return;
            }
            if(md5($pwd)!=$row->pwd){
            	$this->jump("admin/index/index",'密码错误!');
                return;
            }

            if($row->state != 1){
            	$this->jump("admin/index/index",'暂时没有管理员权限!');
                return;
            }
            //把登陆者的info放入session中
            $this->session->set('operator',$row);
            
            //获取登陆者的IP
            $login_ip=$_SERVER["REMOTE_ADDR"];
            //设置时区，将时间戳化成标准形式
            date_default_timezone_set('Asia/Shanghai'); 
            $time=time();
            $login_time=date("Y-m-d,h:i:s",$time);
            $admin_id=$row->id;

            //将登陆者的IP，ID,登陆时间插入数据表
            $map = new AdminLogin();
            $map -> admin_id="$admin_id";
            $map -> login_time="$login_time";
            $map -> login_ip="$login_ip";
            $map -> save();
            
            $this->response->redirect('admin/main/index'); 
            return;   

    	}
    	
    }
   
};
?>